#!/bin/bash

# 景点数据优化一键执行脚本
# 使用方法: bash optimize_attractions.sh

echo "=========================================="
echo "景点数据优化脚本"
echo "=========================================="
echo ""

# 检查是否在虚拟环境中
if [ -z "$VIRTUAL_ENV" ]; then
    echo "警告: 未检测到虚拟环境，建议先激活虚拟环境"
    read -p "是否继续? (y/n) " -n 1 -r
    echo
    if [[ ! $REPLY =~ ^[Yy]$ ]]; then
        exit 1
    fi
fi

# 备份数据库
echo "步骤1: 备份数据库..."
BACKUP_FILE="backup_attractions_$(date +%Y%m%d_%H%M%S).json"
python manage.py dumpdata attractions > "$BACKUP_FILE"
if [ $? -eq 0 ]; then
    echo "✓ 备份成功: $BACKUP_FILE"
else
    echo "✗ 备份失败，请检查数据库连接"
    exit 1
fi
echo ""

# 预览去重操作
echo "步骤2: 预览去重操作..."
python manage.py deduplicate_attractions --dry-run
echo ""

read -p "是否执行去重操作? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    echo "执行去重..."
    python manage.py deduplicate_attractions
    echo "✓ 去重完成"
else
    echo "跳过去重操作"
fi
echo ""

# 丰富各类别数据
echo "步骤3: 丰富各类别景点数据..."
echo "注意: 这可能需要较长时间，请耐心等待"
echo ""

read -p "是否同步自然风光类别? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    python manage.py enrich_attractions_by_category --category natural --max-count 50
fi

read -p "是否同步现代建筑类别? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    python manage.py enrich_attractions_by_category --category modern --max-count 50
fi

read -p "是否同步美食购物类别? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
    python manage.py enrich_attractions_by_category --category food --max-count 50
fi

echo ""
echo "=========================================="
echo "优化完成！"
echo "=========================================="
echo ""
echo "备份文件: $BACKUP_FILE"
echo ""
echo "验证结果:"
python manage.py shell << EOF
from apps.attractions.models import Attraction
from django.db.models import Count

print("\n各类别景点数量:")
for cat in ['cultural', 'natural', 'modern', 'food']:
    count = Attraction.objects.filter(category=cat).count()
    print(f"  {cat}: {count}")

print(f"\n总景点数量: {Attraction.objects.count()}")
EOF

